// pages/detail/detail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    option: {
      flag: 0,
      year: 0,
      money_c: 0,
      money_p: 0,
      commercial: 0,
      providentFund: 0
    },
    table: [],
    //还款总额
    total: 0,
    //利息总额
    total_interest: 0,
    //贷款总额
    total_principal: 0,
    //贷款月数
    total_month: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      option: options
    });
    that.loadData();
  },
  loadData: function () {
    var that = this;
    var flag = that.data.option.flag;
    var year = that.data.option.year;
    var money_c = parseInt(that.data.option.money_c || 0);
    var money_p = parseInt(that.data.option.money_p || 0);
    var commercial = that.data.option.commercial || 0;
    var providentFund = that.data.option.providentFund || 0;
    var result = {};
    if (flag == 0) {              //等额本息
      var result1, result2;
      result1 = result2 = {
        payment: 0,
        total: 0,
        interest: 0
      }
      if (money_c != 0) {
        result1 = that.getResult(year, commercial, money_c);
      }
      if (money_p != 0) {
        result2 = that.getResult(year, providentFund, money_p);
      }
      result = {
        payment: result1.payment + result2.payment,
        total: result1.total + result2.total,
        interest: result1.interest + result2.interest
      }

      var table = [];
      var t1 = money_c * 10000;
      var t2 = money_p * 10000;
      var t = result.total;
      var month_commercial = commercial / 100 / 12;
      var month_providentFund = providentFund / 100 / 12;

      for (var i = 0; i < year; i++) {
        var arr = []
        for (var j = 0; j < 12; j++) {
          var interest = t1 * month_commercial + t2 * month_providentFund;
          var principal = result1.payment + result2.payment - interest;
          var remainder = t - result1.payment - result2.payment;
          var item = {};
          //应还利息
          item.interest = interest.toFixed(2);
          //应还本金
          item.principal = principal.toFixed(2);
          //剩余金额
          item.remainder = Math.abs(remainder).toFixed(2);
          t1 = t1 - (result1.payment - t1 * month_commercial);
          t2 = t2 - (result2.payment - t2 * month_providentFund);
          t = remainder;
          arr.push(item);
        }
        table.push(arr);
      }

      that.setData({
        table: table
      });

    } else if (flag == 1) {       //等额本金
      var result1, result2;
      result1 = result2 = {
        payment: 0,
        total: 0,
        interest: 0
      }
      if (money_c != 0) {
        result1 = that.getResult1(year, commercial, money_c);
      }
      if (money_p != 0) {
        result2 = that.getResult1(year, providentFund, money_p);
      }
      result = {
        payment: result1.payment + result2.payment,
        decline: result1.decline + result2.decline,
        total: result1.total + result2.total,
        interest: result1.interest + result2.interest
      }

      var table = [];
      var t1 = money_c * 10000;
      var t2 = money_p * 10000;
      var t = result.total;
      var principal1 = 0;
      var principal2 = 0;
      var month_commercial = commercial / 100 / 12;
      var month_providentFund = providentFund / 100 / 12;

      for (var i = 0; i < year; i++) {
        var arr = []
        for (var j = 0; j < 12; j++) {
          var principal = money_c * 10000 / year / 12 + money_p * 10000 / year / 12;
          var interest = t1 * month_commercial + t2 * month_providentFund;
          var remainder = t - principal - interest;
          var item = {};
          //应还利息
          item.interest = interest.toFixed(2);
          //应还本金
          item.principal = principal.toFixed(2);
          //剩余金额
          item.remainder = Math.abs(remainder).toFixed(2);
          t1 = t1 - money_c * 10000 / year / 12;
          t2 = t2 - money_p * 10000 / year / 12;
          principal1 += money_c * 10000 / year / 12;
          principal2 += money_p * 10000 / year / 12;
          t = remainder;
          arr.push(item);
        }
        table.push(arr);
      }

      that.setData({
        table: table
      });

    }
    that.setData({
      total: (result.total / 10000).toFixed(2),
      total_interest: (result.total / 10000 - money_c - money_p).toFixed(2),
      total_principal: money_c + money_p,
      total_month: year * 12
    });
  },
  //等额本息公式
  getResult: function (year, rate, money) {
    //还款月数
    var months = year * 12;
    //月利率
    var month_rate = rate / 100 / 12;
    //月供
    var payment = money * 10000 * month_rate * Math.pow(1 + month_rate, months) / (Math.pow(1 + month_rate, months) - 1);
    var total = payment * months;
    //总利息
    var interest = total - money * 10000;
    return {
      payment: payment,
      total: total,
      interest: interest,
    }
  },
  //等额本金公式
  getResult1: function (year, rate, money) {
    //贷款本金
    var p = money * 10000;
    //还款月数
    var months = year * 12;
    //月利率
    var month_rate = rate / 100 / 12;
    //月供
    var payment = (p / months) + p * month_rate;
    //月供递减
    var decline = p / months * month_rate;
    //总利息
    var interest = ((p / months + p * month_rate) + p / months * (1 + month_rate)) / 2 * months - p;
    //还款总额
    var total = interest + p;
    return {
      payment: payment,
      decline: decline,
      total: total,
      interest: interest
    }
  },
})